//
//  DateUtil.m
//  Weike
//
//  Created by leirz on 14-4-10.
//  Copyright (c) 2014年 winupon. All rights reserved.
//

#import "DateUtil.h"

@implementation DateUtil

+ (NSDate *)toDate:(id)millisecond
{
    return [NSDate dateWithTimeIntervalSince1970:[DateUtil getTimeInterval:millisecond]];
}

+ (NSTimeInterval)getTimeInterval:(id)millisecond
{
    long long value = [[NSString stringWithFormat:@"%@", millisecond] longLongValue];
    return value / 1000;
}

+ (NSString *)getTimeStr:(id)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time;
    if ([date isKindOfClass:[NSDate class]]) {
        this_time = [(NSDate *)date timeIntervalSince1970];
    } else {
        this_time = [DateUtil getTimeInterval:date];
    }
    time_t delta = current_time - this_time;
    if (delta <= 0) {
        return @"刚刚";
    } else if (delta < 60) {
        return [NSString stringWithFormat:@"%ld秒前", delta];
    } else if (delta < 3600) {
        return [NSString stringWithFormat:@"%ld分钟前", delta /60];
    } else {
        struct tm tm_now, tm_in;
        localtime_r(&current_time, &tm_now);
        localtime_r(&this_time, &tm_in);
        NSString *format = nil;
        if (tm_now.tm_year == tm_in.tm_year) {
            if (tm_now.tm_yday == tm_in.tm_yday) {
                format = @"今天 %-H:%M";
            } else if (tm_now.tm_yday - tm_in.tm_yday == 1) {
                format = @"昨天 %-H:%M";
            } else {
                format = @"%-m月%-d日 %-H:%M";
            }
        } else {
            format = @"%Y年%-m月%-d日 %-H:%M";
        }
        char buf[256] = {0};
        strftime(buf, sizeof(buf), [format UTF8String], &tm_in);
        return [NSString stringWithUTF8String:buf];
    }
}

+ (NSString *)getTimeSimpleStr:(NSDate *)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time = [(NSDate *)date timeIntervalSince1970];
    struct tm tm_now, tm_in;
    localtime_r(&current_time, &tm_now);
    localtime_r(&this_time, &tm_in);
    NSString *format = nil;
    if (tm_now.tm_year == tm_in.tm_year) {
        if (tm_now.tm_yday == tm_in.tm_yday) {
            format = @"%H:%M";
        } else if (tm_now.tm_yday - tm_in.tm_yday == 1) {
            format = @"昨天 %H:%M";
        } else {
            format = @"%m-%d %H:%M";
        }
    } else {
        format = @"%Y-%m-%d %H:%M";
    }
    char buf[256] = {0};
    strftime(buf, sizeof(buf), [format UTF8String], &tm_in);
    return [NSString stringWithUTF8String:buf];
    
}

+ (NSString *)getTimeStrStyle3:(id)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time;
    if ([date isKindOfClass:[NSDate class]]) {
        this_time = [(NSDate *)date timeIntervalSince1970];
    } else {
        this_time = [DateUtil getTimeInterval:date];
    }
    struct tm tm_now, tm_in;
    localtime_r(&current_time, &tm_now);
    localtime_r(&this_time, &tm_in);
    NSString *format = @"%Y年%-m月%-d日 %H:%M";
    char buf[256] = {0};
    strftime(buf, sizeof(buf), [format UTF8String], &tm_in);
    return [NSString stringWithUTF8String:buf];
}

+ (NSString *)getTimeStrStyle4:(id)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time;
    if ([date isKindOfClass:[NSDate class]]) {
        this_time = [(NSDate *)date timeIntervalSince1970];
    } else {
        this_time = [DateUtil getTimeInterval:date];
    }
    struct tm tm_now, tm_in;
    localtime_r(&current_time, &tm_now);
    localtime_r(&this_time, &tm_in);
    NSString *format = @"%Y年%-m月%-d日";
    char buf[256] = {0};
    strftime(buf, sizeof(buf), [format UTF8String], &tm_in);
    return [NSString stringWithUTF8String:buf];
}
+ (NSString *)getTimeStrStyle5:(id)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time;
    if ([date isKindOfClass:[NSDate class]]) {
        this_time = [(NSDate *)date timeIntervalSince1970];
    } else {
        this_time = [DateUtil getTimeInterval:date];
    }
    struct tm tm_now, tm_in;
    localtime_r(&current_time, &tm_now);
    localtime_r(&this_time, &tm_in);
    NSString *format = @"%Y-%-m-%-d %H:%M";
    char buf[256] = {0};
    strftime(buf, sizeof(buf), [format UTF8String], &tm_in);
    return [NSString stringWithUTF8String:buf];
}

+ (NSDate *)getDateByForamt1:(NSString *) formatStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [formatter dateFromString:formatStr];
}

+ (NSDate *)getDateByForamt6:(NSString *) formatStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    return [formatter dateFromString:formatStr];
}

+ (NSString *)getDateStr:(id)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time;
    if ([date isKindOfClass:[NSDate class]]) {
        this_time = [(NSDate *)date timeIntervalSince1970];
    } else {
        this_time = [DateUtil getTimeInterval:date];
    }
    struct tm tm_now, tm_in;
    localtime_r(&current_time, &tm_now);
    localtime_r(&this_time, &tm_in);
    NSString *format = nil;
    if (tm_now.tm_year == tm_in.tm_year) {
        format = @"%-m月%-d日";
    } else {
        format = @"%Y年%-m月%-d日";
    }
    char buf[256] = {0};
    strftime(buf, sizeof(buf), [format UTF8String], &tm_in);
    return [NSString stringWithUTF8String:buf];

}

+ (NSString *)currentTimeMillis
{
    return [self toMillis:[NSDate date]];
}

+ (NSString *)toMillis:(NSDate *)date
{
    long long millis = [date timeIntervalSince1970] * 1000;
    return [NSString stringWithFormat:@"%lld", millis];
}

+ (NSString *)toString:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [formatter stringFromDate:date];
}

+ (NSString *)toSimpleString:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    return [formatter stringFromDate:date];
}

+ (NSString *)toSimpleString2:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM";
    return [formatter stringFromDate:date];
}

+ (NSString *)toSimpleString3:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yy/MM/dd";
    return [formatter stringFromDate:date];
}

+ (NSString *)toSimpleString1:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm:ss";
    return [formatter stringFromDate:date];
}

+ (NSDate *)getNowDate
{
    NSDate *date = [NSDate date];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate: date];
    return [date dateByAddingTimeInterval: interval];
}

+ (NSDate *)addDay:(NSDate *)date day:(NSInteger)day
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateStr = [formatter stringFromDate:date];
    NSDate *now = [formatter dateFromString:dateStr];
    return [now dateByAddingTimeInterval:24 * 3600 * day];
}

+ (NSDate *)addDay:(NSInteger)day
{
    return [self addDay:[NSDate date] day:day];
}

+ (NSDate *)addMonth:(NSDate *)date count:(NSInteger)monthCount
{
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSDateComponents *cmp = [calender components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];
    [cmp setMonth:[cmp month] + monthCount];
   return  [calender dateFromComponents:cmp];
}

+ (int)differSeconds:(NSDate *)now other:(NSDate *)other
{
    return fabs([now timeIntervalSince1970] - [other timeIntervalSince1970]);
}

+ (BOOL)isSameDay:(NSDate *)date1 date2:(NSDate *)date2
{
    if (date1 == nil || date2 == nil) {
        return NO;
    }
//    NSCalendar* calendar = [NSCalendar currentCalendar];
//    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
//    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
//    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
//    return [comp1 day] == [comp2 day] && [comp1 month] == [comp2 month] && [comp1 year]  == [comp2 year];
   return [DateUtil compareOneDay:date1 withAnotherDay:date2] == 0;
}

+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
    
}

+ (NSArray *)getDateInfo:(id)date
{
    time_t current_time = [[NSDate date] timeIntervalSince1970];
    time_t this_time;
    if ([date isKindOfClass:[NSDate class]]) {
        this_time = [(NSDate *)date timeIntervalSince1970];
    } else {
        this_time = [DateUtil getTimeInterval:date];
    }
    struct tm tm_now, tm_in;
    localtime_r(&current_time, &tm_now);
    localtime_r(&this_time, &tm_in);
    if (tm_now.tm_year == tm_in.tm_year) {
        if (tm_now.tm_yday == tm_in.tm_yday) {
            return [NSArray arrayWithObject:@"今天"];
        } else if (tm_now.tm_yday - tm_in.tm_yday == 1) {
            return [NSArray arrayWithObject:@"昨天"];
        }
    }
    NSString *mday = nil;
    if (tm_in.tm_mday < 10) {
        mday = [NSString stringWithFormat:@"0%d", tm_in.tm_mday];
    } else {
        mday = [NSString stringWithFormat:@"%d", tm_in.tm_mday];
    }
    return [NSArray arrayWithObjects:mday, [NSString stringWithFormat:@"%d",tm_in.tm_mon+1], nil];
}

+ (NSDateComponents *)getTimeInfo:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit) fromDate:date];
}

+ (NSString *)getCurrentTime
{
    return [NSString stringWithFormat:@"%lli", (long long)([[NSDate date] timeIntervalSince1970] * 1000.0)];
}

+ (NSInteger)getWeekday:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday fromDate:date];
    NSInteger weekday = [components weekday];//日-六  对应 1-7
    return weekday-1;
}


+(NSInteger)getDaysCountOfMonth:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

+(NSInteger)getDayWeekOfFirstDayInMonth:(NSDate *)date
{
    NSString *yymm = [DateUtil toSimpleString2:date];
    NSDate *yymm01 = [DateUtil getDateByForamt6:[NSString stringWithFormat:@"%@-01",yymm]];
    NSInteger dayofweek = [DateUtil getWeekday:yymm01];
    return dayofweek;
}



@end
